<?php
/**
 * 测评题目
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-6-17
 * Time: 下午2:37
 */
namespace app\modules\test\controllers;

use app\modules\test\models\TestCate;
use app\modules\test\models\TestList;
use app\modules\test\models\TestListDetail;
use yii;
use app\libs\ApiControl;
use app\modules\exercise\models\ExerciseList;
use app\modules\cn\models\ExerciseTopic;
use app\modules\cn\models\ExerciseLevel;
use app\modules\cn\models\ExerciseType;
use app\modules\cn\models\ExerciseCategory;
use app\modules\exercise\models\ExerciseListDetail;

class QuestionController extends ApiControl
{
    public $layout = 'test';

    public $enableCsrfValidation = false;

    /**
     * @Title  [列表]
     * @Author Don
     * @Date   2021-05-11
     * @return [type]
     */
    public function actionIndex()
    {
        $cateId = Yii::$app->request->get('cate_id', '');
        $type = Yii::$app->request->get('type', '');
        $page = Yii::$app->request->get('page', 1);
        $where = '1=1';
        if ($cateId) {
            $where .= " and cateId=$cateId";
        }
        if ($type) {
            $where .= " and type=$type";
        }
        $model = new TestList();
        $data = $model->getData($where, 15, $page);
        $testPaper = TestCate::find()->asArray()->select('id,name,pid')->where('pid=0')->all();
        $type = ['填空', '单选', '多选'];
        $subject = TestCate::find()->asArray()->select('id,name,pid,type')->where('pid!=0')->all();
        return $this->render('index', [
            'data' => $data,
            'type' => $type,
            'cate' => $testPaper,
            'subject' => $subject,
        ]);
    }

    /**
     * [actionDetail 详情]
     * @Title  [title]
     * @Author Don
     * @Date   2021-05-12
     * @return [type]     [description]
     */
    public function actionDetail()
    {
        $pid = Yii::$app->request->get('pid');
        $list = TestListDetail::find()->asArray()->where('pid='.$pid)->all();
        $pidData = TestList::find()->asArray()->select('tc.name,tl.id')->from('x2_test_list tl')->leftJoin('x2_test_cate tc','tc.id=tl.cateId')->where('tl.id='.$pid)->one();
        $subjectData = TestList::find()->asArray()->select('tc.name,tl.id')->from('x2_test_list tl')->leftJoin('x2_test_cate tc','tc.id=tl.subjectId')->where('tl.id='.$pid)->one();
        return $this->render('detail', [
            'data' => $list,
            'pidData'=>$pidData,
            'subjectData'=>$subjectData,
        ]);
    }


    public function actionAdd()
    {
        if ($_POST) {
            $cateId = Yii::$app->request->post('cateId');
            $id = Yii::$app->request->post('id');
            $name = Yii::$app->request->post('name');
            $subjectId = Yii::$app->request->post('subjectId');
            $question = Yii::$app->request->post('question');
            $option = Yii::$app->request->post('option');
            $article = Yii::$app->request->post('article');
            $questionType = Yii::$app->request->post('questionType');
            $listenFile = Yii::$app->request->post('listenFile');
            $model = new TestList();
            $model->name = $name;
            $model->cateId = $cateId;
            $model->subjectId = $subjectId;
            $model->question = $question;
            $model->option = $option;
            $model->article = $article;
            $model->questionType = $questionType;
            $model->listenFile = $listenFile;
            $model->createTime = time();
            $model->sort = 1;
            if ($id == false) {
                $re = $model->save();
            } else {
                $model->id = $id;
                $re = TestList::updateAll($model, "id=$id");
            }
            if ($re) {
                $this->redirect('/test/question/index');
            } else {
                die('<script>alert("保存失败，请重试");history.go(-1);</script>');
            }
        } else {
            // 题型
            $id = Yii::$app->request->get('id');
            $testPaper = TestCate::find()->asArray()->select('id,name,pid')->where('pid=0')->all();
            $type = ['填空', '单选', '多选'];
            $subject = TestCate::find()->asArray()->select('id,name,pid,type')->where('pid!=0')->all();
            if ($id) {
                $data = TestList::find()->asArray()->where('id=' . $id)->one();
                $testPaper = TestCate::find()->asArray()->select('id,name,pid')->where('id='.$data['cateId'])->all();
                $subject = TestCate::find()->asArray()->select('id,name,pid,type')->where('pid='.$data['cateId'])->all();

            } else {
                $data = '';
            }
            return $this->render('add', [
                'data' => $data,
                'option' => $testPaper,
                'type' => $type,
                'subject' => $subject,
            ]);
        }
    }

    public function actionAddDetail()
    {
        if ($_POST) {
            $id = Yii::$app->request->post('id');
            $pid = Yii::$app->request->post('pid');
            $name = Yii::$app->request->post('name');
            $answer = Yii::$app->request->post('answer');
            $article = Yii::$app->request->post('article');
            $option = Yii::$app->request->post('option');
            $analysis = Yii::$app->request->post('analysis');
            $cn = Yii::$app->request->post('cn');
            $model = new TestListDetail();
            $model->pid = $pid;
            $model->name = $name;
            $model->answer = $answer;
            $model->article = $article;
            $model->createTime = time();
            $model->option = $option;
            $model->analysis = $analysis;
            $model->cn = $cn;
            if ($id == false) {
                $re=$model->save();
            } else {
                $model->id = $id;
                $re = TestListDetail::updateAll($model, "id=$id");
            }
            if ($re) {
                $this->redirect('/test/question/detail?pid=' . $pid);
            } else {
                die('<script>alert("保存失败，请重试");history.go(-1);</script>');
            }

        } else {
            $id = Yii::$app->request->get('id');
            if ($id != false) {
                $data = TestListDetail::findOne($id);
            } else {
                $data = '';
            }
            return $this->render('add_detail', ['id' => $id, 'data' => $data]);
        }
    }
}